package com.example.basemvvmkotlin

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.lib_base.base.http.ApiResponse
import com.example.basemvvmkotlin.net.InfoBean
import com.example.basemvvmkotlin.net.MyApiClient
import com.example.basemvvmkotlin.net.NewsListBean
import com.example.basemvvmkotlin.net.UserApiService
import com.example.basemvvmkotlin.ui.HomeActivity
import com.example.basemvvmkotlin.ui.TestRvActivity
import com.example.lib_base.base.view.LoadDialog
import com.example.lib_base.base.view.TitleBar

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<TextView>(R.id.tv).setOnClickListener {

//            showShotToast("跳转")
            startActivity(Intent(this, HomeActivity::class.java))
        }
        findViewById<TextView>(R.id.tv2).setOnClickListener {

//            showShotToast("跳转")
            startActivity(Intent(this, TestRvActivity::class.java))
        }

        findViewById<TextView>(R.id.tv3).setOnClickListener {
//            startActivity(Intent(this, FragmentRvActivity::class.java))
            LoadDialog.show(this)
        }
        var bar = TitleBar(this)

    }


    suspend fun textApi() {

//        ApiClient.getInstance().requestSafely(UserApiService::class.java) {
//            it.hotPushList()
//        }.doSuccess { bean, code, msg ->
//            println("----------------------- 成功 code = $code")
//            println("----------------------- 成功 msg = $msg")
//            println("----------------------- 成功 code = ${bean.toString()}")
//        }.doFailure { code, msg -> //失败
//            println("----------------- 失败 错误码 = $code  msg=$msg ")
//        }.doError { ex, error -> //错误
//            println("----------------- 错误 ex = $ex  error=$error ")
//        }.doCancel { //请求取消
//        }.procceed()

//        startApi(UserApiService::class.java)
//
//        ApiClient.getInstance().requestSafely(UserApiService::class.java){
//            val map:Map<String,String> = mutableMapOf("page" to "1")
//            it.newsListBean(map)
//        }.doSuccess { bean, code, msg ->
//            println("----------------------- 成功 code = $code")
//            println("----------------------- 成功 msg = $msg")
//            println("----------------------- 成功 code = ${bean.toString()}")
//        }.doFailure { code, msg -> //失败
//            println("----------------- 失败 错误码 = $code  msg=$msg ")
//        }.doError { ex, error -> //错误
//            println("----------------- 错误 ex = $ex  error=$error ")
//        }.doCancel { //请求取消
//
//        }.procceed()
        executeResp {
            println("-------------------------------body = $it")
//            ApiClient.getInstance().getService(UserApiService::class.java).newsListBean(mutableMapOf(
//                "page" to "1000" ,
//                "snapshotID" to "hotPushSnapshot_202107091042"))
            MyApiClient.getInstance().getService(UserApiService::class.java).hotPushList()
        }
    }

    private suspend fun <T : Any> executeResp(
        block: suspend (body:String) -> ApiResponse<T>
    ){
        try {
            var bean = block.invoke("sssss")
            if (bean.isSuccess()){
                setData(bean,bean.code,bean.message)
            }else{
                onError(bean.code,bean.message)
            }
        }catch (e:Throwable){
            println("---------------- 异常 ")
            onCatch()
        }finally {
            println("---------------- 结束")
        }
    }

     fun <T : Any> setData(bean: T, code:Int, msg:String){
         println("---------------- 成功 ${bean::class.java.canonicalName}")
         //ApiResponse
         if (bean is ApiResponse<*>){
             println("------------------- 类型 ApiResponse")

             when (bean.data){
                 is NewsListBean -> println("----------------- 列表数据")
                 is InfoBean -> println("------------------- 版本检测")
                 else -> {
                     println("------------------- 未定义错误类型")
                 }
             }

         }
     }

    //错误
    fun onError(code:Int,msg:String){
        println("---------------- 错误 code = $code")
        println("---------------- 错误 msg  = $msg")
    }
    //异常
    fun onCatch(){
        println("---------------- 异常 ")
    }

    suspend fun <T : UserApiService>startApi(apiInterface: Class<T>){
//        UserApiService

        MyApiClient.getInstance().getService(UserApiService::class.java).hotPushList()
        try {
            var bean =  MyApiClient.getInstance().getService(apiInterface).newsListBean(
                mutableMapOf("page" to "1"))
            println("---------------------1 ${bean.code}")
            println("---------------------2 ${bean.message}")
            println("---------------------3 ${bean.data.toString()}")
        }catch (e:Throwable){
           println("---------------- 异常 ")
        }finally {
           println("---------------- 结束")
        }

//      var bean =  ApiClient.getInstance().getService(UserApiService::class.java).newsListBean(
//          mutableMapOf("" to ""))
//        println("---------------------1 ${bean.code}")
//        println("---------------------2 ${bean.message}")
//        println("---------------------3 ${bean.data.toString()}")


//        ApiClient.getInstance().requestSafely(apiInterface){
////            val map:Map<String,String> = mutableMapOf("page" to "1")
////            it.newsListBean(map)
//             //返回结果
//             ApiResponse<InfoBean>(0,"ok",null)
//
//        }.doSuccess { bean, code, msg ->
//            println("------------------- 成功")
//
//        }.doFailure { code, msg -> //失败
//            println("------------------- 失败")
//        }.doError { ex, error -> //错误
//            println("------------------- error")
//        }.doCancel { //请求取消
//        }.procceed()
    }

//    suspend fun <T:UserApiService,D : Any>getData(apiInterface:T) : D {
////        return apiInterface.hotPushList()
//    }


}